var util = require('../../utils/util');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import {$wuxLoading} from '../../dist/index';

var App = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	util.loading = $wuxLoading();
	
  },

  /**
   * 授权登录
   */
  authorLogin: function (e) {
    that = this;
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }

    util.showLoading();
    // 执行微信登录
    wx.login({
      success: function (res) {
		  
        var url = config.login;
        var params = {
          code: res.code,
                info: e.detail.rawData,
                encrypted_data: e.detail.encryptedData,
                iv: e.detail.iv,
                signature: e.detail.signature
        }
        wxRequest.postRequest(url, params) 
        .then(res=>{
          if(util.judge.isObject(res)){
          res.user.token=res.token;
          //that.data.user = obj;
          wx.setStorageSync('user', res.user);
          wx.setStorageSync('token', res.token);
          // 跳转回原页面
              
          wx.switchTab({
            url: '../index/index',
            success: function (e) {

            }
          });
          }
        })
        .catch(res => {
            util.alert(res.message);
        })
        .finally(function (res) {
          util.hideLoading();
          console.log('app.getuser.finally~')
        });

      }
    })
	
  },

  /**
   * 授权成功 跳转回原页面
   */
  navigateBack: function () {
    wx.navigateBack();
    // let currentPage = wx.getStorageSync('currentPage');
    // wx.redirectTo({
    //   url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
    // });
  },

})